package lucky.xiong.djstl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.ArrayList;
import java.util.List;

/**
 * 仅业务需要，不涉及任何算法
 * @author XiongJianLin
 * @date Created in 2022/3/16 13:58
 */
public class 自用小工具 {

    public static void main(String[] args) {
        String   pointJson = "[{\"id\":\"A8ND6e\",\"x\":3024,\"y\":360,\"distance\":[{\"id\":\"MbWaN4\",\"dis\":947},{\"id\":\"RDGRQm\",\"dis\":958},{\"id\":\"7M6bGY\",\"dis\":527}]},{\"id\":\"MbWaN4\",\"x\":2080,\"y\":280,\"distance\":[{\"id\":\"A8ND6e\",\"dis\":947}]},{\"id\":\"RDGRQm\",\"x\":3980,\"y\":420,\"distance\":[{\"id\":\"A8ND6e\",\"dis\":958}]},{\"id\":\"7M6bGY\",\"x\":2968,\"y\":884,\"distance\":[{\"id\":\"A8ND6e\",\"dis\":527},{\"id\":\"E4ePHK\",\"dis\":404},{\"id\":\"QsYrjB\",\"dis\":1077}]},{\"id\":\"E4ePHK\",\"x\":2564,\"y\":876,\"distance\":[{\"id\":\"7M6bGY\",\"dis\":404},{\"id\":\"tGTAyj\",\"dis\":1025}]},{\"id\":\"tGTAyj\",\"x\":2484,\"y\":1898,\"distance\":[{\"id\":\"E4ePHK\",\"dis\":1025},{\"id\":\"QsYrjB\",\"dis\":404}]},{\"id\":\"QsYrjB\",\"x\":2884,\"y\":1958,\"distance\":[{\"id\":\"tGTAyj\",\"dis\":404},{\"id\":\"7M6bGY\",\"dis\":1077},{\"id\":\"sfXmf5\",\"dis\":332}]},{\"id\":\"sfXmf5\",\"x\":2878,\"y\":2290,\"distance\":[{\"id\":\"QsYrjB\",\"dis\":332},{\"id\":\"PPKkNn\",\"dis\":372},{\"id\":\"kRftYA\",\"dis\":1182}]},{\"id\":\"PPKkNn\",\"x\":3250,\"y\":2304,\"distance\":[{\"id\":\"sfXmf5\",\"dis\":372},{\"id\":\"t56Gab\",\"dis\":283}]},{\"id\":\"t56Gab\",\"x\":3510,\"y\":2192,\"distance\":[{\"id\":\"PPKkNn\",\"dis\":283},{\"id\":\"627sx6\",\"dis\":268}]},{\"id\":\"627sx6\",\"x\":3778,\"y\":2196,\"distance\":[{\"id\":\"t56Gab\",\"dis\":268},{\"id\":\"7ax4Kn\",\"dis\":387},{\"id\":\"Dh2b36\",\"dis\":285}]},{\"id\":\"7ax4Kn\",\"x\":3808,\"y\":1810,\"distance\":[{\"id\":\"627sx6\",\"dis\":387},{\"id\":\"aNfYJj\",\"dis\":399},{\"id\":\"4WkC8p\",\"dis\":861},{\"id\":\"EYG2jf\",\"dis\":450}]},{\"id\":\"aNfYJj\",\"x\":3412,\"y\":1758,\"distance\":[{\"id\":\"7ax4Kn\",\"dis\":399},{\"id\":\"R5rsfX\",\"dis\":858}]},{\"id\":\"R5rsfX\",\"x\":3466,\"y\":902,\"distance\":[{\"id\":\"aNfYJj\",\"dis\":858},{\"id\":\"4WkC8p\",\"dis\":453}]},{\"id\":\"4WkC8p\",\"x\":3916,\"y\":956,\"distance\":[{\"id\":\"R5rsfX\",\"dis\":453},{\"id\":\"6hftWi\",\"dis\":503},{\"id\":\"7ax4Kn\",\"dis\":861}]},{\"id\":\"6hftWi\",\"x\":4312,\"y\":1266,\"distance\":[{\"id\":\"4WkC8p\",\"dis\":503}]},{\"id\":\"EYG2jf\",\"x\":4256,\"y\":1852,\"distance\":[{\"id\":\"7ax4Kn\",\"dis\":450},{\"id\":\"GdFw2t\",\"dis\":553}]},{\"id\":\"GdFw2t\",\"x\":4554,\"y\":2318,\"distance\":[{\"id\":\"EYG2jf\",\"dis\":553},{\"id\":\"ix3SeH\",\"dis\":235}]},{\"id\":\"ix3SeH\",\"x\":4764,\"y\":2424,\"distance\":[{\"id\":\"GdFw2t\",\"dis\":235},{\"id\":\"PJf3CG\",\"dis\":334}]},{\"id\":\"PJf3CG\",\"x\":4836,\"y\":2750,\"distance\":[{\"id\":\"ix3SeH\",\"dis\":334}]},{\"id\":\"Dh2b36\",\"x\":4060,\"y\":2238,\"distance\":[{\"id\":\"627sx6\",\"dis\":285},{\"id\":\"dpSya7\",\"dis\":415}]},{\"id\":\"dpSya7\",\"x\":4274,\"y\":2594,\"distance\":[{\"id\":\"Dh2b36\",\"dis\":415},{\"id\":\"T6FEQW\",\"dis\":654},{\"id\":\"dBnHpe\",\"dis\":319}]},{\"id\":\"T6FEQW\",\"x\":4602,\"y\":3160,\"distance\":[{\"id\":\"dpSya7\",\"dis\":654},{\"id\":\"Tb2E6A\",\"dis\":1962},{\"id\":\"h6frYk\",\"dis\":839}]},{\"id\":\"dBnHpe\",\"x\":3992,\"y\":2744,\"distance\":[{\"id\":\"dpSya7\",\"dis\":319},{\"id\":\"sSE3F6\",\"dis\":1350}]},{\"id\":\"sSE3F6\",\"x\":2642,\"y\":2748,\"distance\":[{\"id\":\"dBnHpe\",\"dis\":1350},{\"id\":\"Tb2E6A\",\"dis\":398},{\"id\":\"ad8Z56\",\"dis\":940}]},{\"id\":\"Tb2E6A\",\"x\":2640,\"y\":3146,\"distance\":[{\"id\":\"sSE3F6\",\"dis\":398},{\"id\":\"T6FEQW\",\"dis\":1962},{\"id\":\"dNwnJG\",\"dis\":1778},{\"id\":\"ebGYNt\",\"dis\":534}]},{\"id\":\"h6frYk\",\"x\":5430,\"y\":3024,\"distance\":[{\"id\":\"T6FEQW\",\"dis\":839},{\"id\":\"ABQxks\",\"dis\":985},{\"id\":\"XiHNB8\",\"dis\":1230}]},{\"id\":\"ABQxks\",\"x\":5226,\"y\":2060,\"distance\":[{\"id\":\"h6frYk\",\"dis\":985}]},{\"id\":\"XiHNB8\",\"x\":5672,\"y\":4230,\"distance\":[{\"id\":\"h6frYk\",\"dis\":1230}]},{\"id\":\"kRftYA\",\"x\":1712,\"y\":2482,\"distance\":[{\"id\":\"sfXmf5\",\"dis\":1182},{\"id\":\"fHHFAE\",\"dis\":432},{\"id\":\"ad8Z56\",\"dis\":250}]},{\"id\":\"fHHFAE\",\"x\":1284,\"y\":2420,\"distance\":[{\"id\":\"kRftYA\",\"dis\":432}]},{\"id\":\"ad8Z56\",\"x\":1702,\"y\":2732,\"distance\":[{\"id\":\"kRftYA\",\"dis\":250},{\"id\":\"sSE3F6\",\"dis\":940},{\"id\":\"maaQtB\",\"dis\":816}]},{\"id\":\"maaQtB\",\"x\":886,\"y\":2758,\"distance\":[{\"id\":\"ad8Z56\",\"dis\":816},{\"id\":\"dNwnJG\",\"dis\":371}]},{\"id\":\"dNwnJG\",\"x\":862,\"y\":3128,\"distance\":[{\"id\":\"maaQtB\",\"dis\":371},{\"id\":\"Tb2E6A\",\"dis\":1778}]},{\"id\":\"ebGYNt\",\"x\":2636,\"y\":3680,\"distance\":[{\"id\":\"Tb2E6A\",\"dis\":534},{\"id\":\"55tC2z\",\"dis\":314},{\"id\":\"Qxa86R\",\"dis\":674},{\"id\":\"ZYTFrr\",\"dis\":426}]},{\"id\":\"55tC2z\",\"x\":2950,\"y\":3686,\"distance\":[{\"id\":\"ebGYNt\",\"dis\":314}]},{\"id\":\"Qxa86R\",\"x\":1962,\"y\":3680,\"distance\":[{\"id\":\"ebGYNt\",\"dis\":674}]},{\"id\":\"ZYTFrr\",\"x\":2624,\"y\":4106,\"distance\":[{\"id\":\"ebGYNt\",\"dis\":426},{\"id\":\"8x2inD\",\"dis\":1766},{\"id\":\"TSnyMp\",\"dis\":1058}]},{\"id\":\"8x2inD\",\"x\":858,\"y\":4144,\"distance\":[{\"id\":\"ZYTFrr\",\"dis\":1766},{\"id\":\"aRnizk\",\"dis\":404}]},{\"id\":\"aRnizk\",\"x\":848,\"y\":4548,\"distance\":[{\"id\":\"8x2inD\",\"dis\":404},{\"id\":\"36RaQz\",\"dis\":418},{\"id\":\"jzSXYK\",\"dis\":1786}]},{\"id\":\"36RaQz\",\"x\":854,\"y\":4966,\"distance\":[{\"id\":\"aRnizk\",\"dis\":418},{\"id\":\"64Dzfp\",\"dis\":1186}]},{\"id\":\"jzSXYK\",\"x\":2634,\"y\":4552,\"distance\":[{\"id\":\"aRnizk\",\"dis\":1786},{\"id\":\"3iPbkf\",\"dis\":1068},{\"id\":\"DnzRBh\",\"dis\":1170}]},{\"id\":\"3iPbkf\",\"x\":3702,\"y\":4540,\"distance\":[{\"id\":\"jzSXYK\",\"dis\":1068},{\"id\":\"TSnyMp\",\"dis\":412},{\"id\":\"j6MAMG\",\"dis\":408}]},{\"id\":\"TSnyMp\",\"x\":3682,\"y\":4128,\"distance\":[{\"id\":\"3iPbkf\",\"dis\":412},{\"id\":\"ZYTFrr\",\"dis\":1058}]},{\"id\":\"j6MAMG\",\"x\":3696,\"y\":4948,\"distance\":[{\"id\":\"3iPbkf\",\"dis\":408},{\"id\":\"p5AWDW\",\"dis\":745}]},{\"id\":\"p5AWDW\",\"x\":2956,\"y\":5032,\"distance\":[{\"id\":\"j6MAMG\",\"dis\":745},{\"id\":\"xMWA7b\",\"dis\":690}]},{\"id\":\"xMWA7b\",\"x\":2910,\"y\":5720,\"distance\":[{\"id\":\"p5AWDW\",\"dis\":690},{\"id\":\"DnzRBh\",\"dis\":280}]},{\"id\":\"DnzRBh\",\"x\":2630,\"y\":5722,\"distance\":[{\"id\":\"xMWA7b\",\"dis\":280},{\"id\":\"jzSXYK\",\"dis\":1170},{\"id\":\"rrWM87\",\"dis\":596}]},{\"id\":\"rrWM87\",\"x\":2034,\"y\":5712,\"distance\":[{\"id\":\"DnzRBh\",\"dis\":596},{\"id\":\"QBA6x6\",\"dis\":398}]},{\"id\":\"QBA6x6\",\"x\":2038,\"y\":5314,\"distance\":[{\"id\":\"rrWM87\",\"dis\":398},{\"id\":\"QhGEks\",\"dis\":444},{\"id\":\"64Dzfp\",\"dis\":336}]},{\"id\":\"QhGEks\",\"x\":2482,\"y\":5316,\"distance\":[{\"id\":\"QBA6x6\",\"dis\":444}]},{\"id\":\"64Dzfp\",\"x\":2040,\"y\":4978,\"distance\":[{\"id\":\"QBA6x6\",\"dis\":336},{\"id\":\"Jaa5He\",\"dis\":440},{\"id\":\"36RaQz\",\"dis\":1186}]},{\"id\":\"Jaa5He\",\"x\":2480,\"y\":4972,\"distance\":[{\"id\":\"64Dzfp\",\"dis\":440}]}]";
        JSONArray parse =  JSONArray.parseArray(pointJson);
        List<Point> points = new ArrayList<>();
        parse.forEach(e-> {
            JSONObject json = (JSONObject) JSONObject.toJSON(e);
            Integer x = json.getInteger("x");
            Integer y = json.getInteger("y");
            String id = json.getString("id");
            // JSONArray 转换 List<T>
            List<Distance> distance = JSONObject.parseArray(json.getString("distance"), Distance.class);
            Point point = new Point();
            point.setDistance(distance).setId(id).setX(x).setY(y);
            points.add(point);
        });
        points.forEach(System.out::println);


        /**
         * 转换成三维数组
         * [
         *   [
         *      [当前x,y]  --p1
         *      [可达x,y]  --p10
         *      [距离dx,dy]
         *   ]
         *   [
         *      [当前x,y]  -- p1
         *      [可达x,y]  -- p9
         *      [距离dx,dy]
         *   ]
         * ]
         *
         * 规则： 第二位数组表示某个点可达所有的点。
         * x,y不可达用null表示
         *
         * 实际实例钟有所不同。 只求最大的两个距离。
         *
         * [
         *     [[380,530],[380,820],[290,null]],
         *     [[380,530],[1290,530],[null,910]],
         *     [[380,820],[380,1360],[540,540]],
         *     [[380,820],[1290,820],[910,910]],
         *     [[1290,530],[1290,820],[null,290]],
         *     [[380,1360],[770,1360],[390,390]],
         *     [[380,1360],[380,1670],[310,310]],
         *     [[380,1670],[970,1670],[590,590]],
         *     [[970,1670],[970,1980],[310,310]],
         *     [[970,1670],[3060,1670],[2090,2090]],
         *     [[970,1980],[380,1980],[590,590]],
         *     [[380,1980],[380,2280],[300,300]],
         *     [[380,2280],[380,2580],[300,300]],
         *     [[380,2280],[1360,2280],[980,980]],
         *     [[380,2580],[1360,2580],[980,980]],
         *     [[1360,2280],[1360,2580],[300,300]],
         *     [[1360,2280],[1660,2280],[300,300]],
         *     [[1660,2280],[1660,2950],[300,300]],
         *     [[1660,2280],[1880,2280],[220,220]],
         *     [[1880,2280],[1880,2580],[300,300]],
         *     [[1880,2280],[2570,2280],[690,690]],
         *     [[1880,2580],[3060,2580],[1180,1180]],
         *     [[3060,2580],[3060,2280],[300,300]],
         *     [[3060,2280],[2570,2280],[500,500]],
         *     [[2570,2280],[2570,1980],[300,300]],
         *     [[2570,1980],[3060,1980],[490,490]],
         *     [[3060,1980],[3060,1670],[310,310]],
         *     [[3060,1670],[3060,1360],[310,310]],
         *     [[3060,1360],[2660,1360],[400,400]],
         *     [[2660,1360],[770,1360],[1890,1890]],
         *     [[3060,1360],[3060,820],[540,540]],
         *     [[3060,820],[2320,820],[null,740]],
         *     [[2320,820],[2320,530],[null,440]],
         *     [[2320,530],[2910,530],[590,590]],
         *     [[3060,820],[3060,530],[290,290]],
         *     [[2190,530],[2320,530],[130,null]],
         *     [[2450,1980],[2570,1980],[120,null]],
         *     [[1750,2580],[1880,2580],[130,null]]
         *
         * ]
         */
        List<Integer[][]> res = new ArrayList<>();

        points.forEach(one->{
            // 获取每一个的可达点以及距离
            List<Distance> distance = one.getDistance();
            distance.forEach(dis->{
                String id = dis.getId();
                // 获取每个可达点的坐标
                Point target = points.stream().filter(p -> p.getId().equals(id)).findFirst().orElse(null);
                // 保存一组数据
                Integer[][] value = new Integer[3][2];
                value[0] = new Integer[]{one.getX(), one.getY()};
                value[1] = new Integer[]{target.getX(), target.getY()};
                value[2] = new Integer[]{dis.getDis(),dis.getDis()};
                res.add(value);
            });
        });

        JSONArray o = (JSONArray) JSONArray.toJSON(res);
        System.out.println(o.toJSONString());

    }



    @Data
    @Accessors(chain = true)
    static class Point{
        String id;
        Integer x;
        Integer y;
        // 可达点
        List<Distance> distance;
    }
    @Data
    static class Distance {
        String id;
        Integer dis;
    }
}
